package com.neusoft.neuiotms.dcom.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.converter.UserAuthorityConditionConverter;
import com.neusoft.neuiotms.dcom.dto.FunctionalGroupDTO;
import com.neusoft.neuiotms.dcom.dto.FunctionalUserDTO;
import com.neusoft.neuiotms.dcom.service.FunctionalGroupService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "职能管理")
@RestController
@RequestMapping(value = "/api/w/functionalgroups")
public class FunctionalGroupController {

    @Autowired
    private FunctionalGroupService functionalGroupService;
    @Autowired
    private UserAuthorityConditionConverter userAuthorityConditionConverter;

    @ApiOperation("查询所有职能组")
    @GetMapping
    public ResultListDTO<FunctionalGroupDTO> index(final Searchable searchable, final UserBean user) {
        this.userAuthorityConditionConverter.addUserDownOrgCodesAuthoritySearchCondition(searchable, user);
        return ResultListDTO.success(this.functionalGroupService.index(searchable));
    }

    @ApiOperation("分页查询所有职能组")
    @GetMapping("/p")
    public PageResultDTO<FunctionalGroupDTO> search(final Pageable pageable, final Searchable searchable,
            final UserBean user) {
        // 添加登录人的组织全县查询条件
        this.userAuthorityConditionConverter.addUserDownOrgCodesAuthoritySearchCondition(searchable, user);
        return PageResultDTO.success(this.functionalGroupService.search(pageable, searchable));
    }

    @ApiOperation("分页查询职能组人员信息")
    @GetMapping("/users/p")
    public PageResultDTO<FunctionalUserDTO> userSearch(final Pageable pageable, final Searchable searchable) {
        // 添加登录人的组织全县查询条件
        return PageResultDTO.success(this.functionalGroupService.userSearch(pageable, searchable));
    }

    @ApiOperation("创建职能组")
    @PostMapping("")
    public ResultDTO<FunctionalGroupDTO> create(@RequestBody final FunctionalGroupDTO dto) {
        return ResultDTO.success(this.functionalGroupService.create(dto));
    }

    @ApiOperation(value = "修改职能组")
    @PutMapping("/{id}")
    public ResultDTO<FunctionalGroupDTO> update(@PathVariable final Long id,
            @RequestBody final FunctionalGroupDTO dto) {
        return ResultDTO.success(this.functionalGroupService.update(id, dto));
    }

    @ApiOperation("职能组详情")
    @GetMapping("/{id}")
    public ResultDTO<FunctionalGroupDTO> details(@PathVariable final Long id) {
        return ResultDTO.success(this.functionalGroupService.details(id));
    }

    @ApiOperation("删除职能组")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.functionalGroupService.delete(id);
        return ResultDTO.success();
    }
}
